<?php

namespace app\admin\controller\questionnaire\config;

use app\BaseController;
use app\model\questionnaire\QuestionnaireTypeModel;
use think\App;

class Type extends BaseController
{
    protected $mode;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->mode = new QuestionnaireTypeModel();
    }

    /**
     * 分类列表
     * @return bool|string
     */
    public function list(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize' => 'require',
            ]);
            $where = [];
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $data = $this->mode->where($where)->order('sort','asc')->paginate($param['pageSize'])->toArray();
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型添加
     * @return bool|string
     */
    public function add(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'name' => 'require',
                'sort' => 'require|number',
            ]);
            $param['unit_id'] = $this->userData['unit_id'];
            $param['time'] = time();
            $this->mode->insert($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型编辑
     * @return bool|string
     */
    public function update(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id' => 'require|number',
                'name' => 'require',
                'sort' => 'require|number',
            ]);
            $info = $this->mode->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info->save($param);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 类型删除
     * @return bool|string
     */
    public function del(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $info = $this->mode->find($id);
            if (!$info) throw new \Exception('参数错误！');
            $info->save([
                'is_del' => 1,
                'del_time' => time()
            ]);
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }
}